package de.bazinga.rotw.effects;

import com.jme.renderer.Renderer;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.scene.shape.Quad;
import com.jme.scene.state.BlendState;

import de.bazinga.rotw.game.Game;
import de.bazinga.rotw.texture.TextureCache;

public class Nightvision {
	
	private static Nightvision instance = null;

	
	private Node rootNode = null;
	private Quad fullScreenQuad = null;
	
	
	private Nightvision() {
		
	}
	
	
	public static Nightvision getInstance() {
		if(instance == null) instance = new Nightvision();
		return instance;
	}
	
	
	public void init(Node rootNode) {
		this.rootNode = rootNode;
		
		fullScreenQuad = new Quad("Nightvision", 
				Game.getGame().getDisplay().getWidth(), 
				Game.getGame().getDisplay().getHeight());
        fullScreenQuad.getLocalRotation().set(0, 0, 0, 1);
        fullScreenQuad.getLocalTranslation().set(
        		Game.getGame().getDisplay().getWidth() / 2, 
        		Game.getGame().getDisplay().getHeight() / 2, 
        		0);
        fullScreenQuad.getLocalScale().set(1, 1, 1);
        fullScreenQuad.setRenderQueueMode(Renderer.QUEUE_ORTHO);
        fullScreenQuad.setCullHint(Spatial.CullHint.Never);
        fullScreenQuad.setTextureCombineMode(Spatial.TextureCombineMode.Replace);
        fullScreenQuad.setLightCombineMode(Spatial.LightCombineMode.Off);

//either with a color like this:
        //fullScreenQuad.setSolidColor(new ColorRGBA(0.1f,1.0f,0.1f,1.0f));
//or with a texture like this:
        fullScreenQuad.setRenderState(TextureCache.getInstance().getTexture("texture/nightvision.png"));

        
        BlendState bs = Game.getGame().getDisplay().getRenderer().createBlendState();
        bs.setBlendEnabled(true);
        bs.setSourceFunction(BlendState.SourceFunction.SourceAlpha);
        bs.setDestinationFunction(BlendState.DestinationFunction.One);
        bs.setTestEnabled(true);
        fullScreenQuad.setRenderState(bs);

        fullScreenQuad.updateRenderState();
	}
	
	public void switchEnabled() {
		if(rootNode.hasChild(fullScreenQuad)) {
			rootNode.detachChild(fullScreenQuad);
		}else{
			rootNode.attachChild(fullScreenQuad);
		}
		rootNode.updateRenderState();
	}
}
